<?php

namespace App\Http\Logic\Api;

use App\Models\AdminConfig;
use App\Models\AdminApplyDraft;
use Exception;

class ApplyDraftLogic
{
    /**
     * @throws Exception
     */
    public function create($params, $user): array
    {
        $work_type = AdminConfig::find($params['work_type']);
        $level = AdminConfig::find($params['level']);
        $source =  AdminConfig::find($params['source']);
        return AdminApplyDraft::query()->create([
            'user_id' => $user['user_id'],
            'user_name' => $params['name'],
            'id_card_number' => $params['id_card_number'],
            'phone' => $params['phone'],
            'source' => $source['name'] ?? '',
            'company' => $params['company'] ?? '',
            'work_type' => $work_type['name'] ?? '',
            'level' => $level['name'] ?? '',
            'age'=>$params['age'],
            'sex'=>$params['sex'],
            'work_experience' => $params['work_experience'],
            'card_front_image' => $params['card_front_image'],
            'card_back_image' => $params['card_back_image'],
            'head_image' => $params['head_image'] ?? '',
            'diploma_image' => $params['diploma_image'] ?? '',
            'social_security_image' => $params['social_security_image'] ?? '',
            'skill_image' => $params['skill_image'] ?? '',
        ])->toArray();
    }

    public function list($user): array
    {
        return AdminApplyDraft::query()->where('user_id', $user['user_id'])->get()->toArray();
    }

    /**
     * @throws Exception
     */
    public function detail($params): array
    {
        $info = AdminApplyDraft::query()->find($params['id']);
        if (empty($info)){
            throw new Exception('数据不存在');
        }
        return $info->toArray();
    }
}
